<template>
    <div class="admin-dashboard">
        <!-- 标题 -->
        <h1 class="dashboard-title">数据统计</h1>

        <!-- 统计卡片 -->
        <el-row :gutter="20">
            <!-- 用户数量卡片 -->
            <el-col :span="6">
                <el-card shadow="hover" class="stats-card">
                    <template #header>
                        <div class="card-header">
                            <el-icon><User /></el-icon>
                            <span>用户数量</span>
                        </div>
                    </template>
                    <div class="card-value">{{ stats.userCount }}</div>
                </el-card>
            </el-col>

            <!-- 宠物数量卡片 -->
            <el-col :span="6">
                <el-card shadow="hover" class="stats-card">
                    <template #header>
                        <div class="card-header">
                            <el-icon></el-icon>
                            <span>宠物数量</span>
                        </div>
                    </template>
                    <div class="card-value">{{ stats.petCount }}</div>
                </el-card>
            </el-col>

            <!-- 文章数量卡片 -->
            <el-col :span="6">
                <el-card shadow="hover" class="stats-card">
                    <template #header>
                        <div class="card-header">
                            <el-icon><Document /></el-icon>
                            <span>文章数量</span>
                        </div>
                    </template>
                    <div class="card-value">{{ stats.articleCount }}</div>
                </el-card>
            </el-col>

            <!-- 讨论区帖子数量卡片 -->
            <el-col :span="6">
                <el-card shadow="hover" class="stats-card">
                    <template #header>
                        <div class="card-header">
                            <el-icon><ChatDotRound /></el-icon>
                            <span>讨论区帖子数量</span>
                        </div>
                    </template>
                    <div class="card-value">{{ stats.postCount }}</div>
                </el-card>
            </el-col>
        </el-row>

        <!-- 统计图表 -->
        <el-row :gutter="20" class="charts-row">
            <el-col :span="12">
                <el-card shadow="hover" class="chart-card">
                    <template #header>
                        <div class="card-header">
                            <span>数据分布饼图</span>
                        </div>
                    </template>
                    <div ref="pieChartRef" class="chart"></div>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card shadow="hover" class="chart-card">
                    <template #header>
                        <div class="card-header">
                            <span>数据分布柱状图</span>
                        </div>
                    </template>
                    <div ref="barChartRef" class="chart"></div>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script setup>
import { ref, onMounted, watch,onUnmounted } from 'vue';
import { ElMessage } from 'element-plus';
import { User, Document, ChatDotRound } from '@element-plus/icons-vue';
import { get_stats } from '@/api/home';
import * as echarts from 'echarts';

// 初始化统计数据
const stats = ref({
    userCount: 0,
    petCount: 0,
    articleCount: 0,
    postCount: 0
});

// 图表引用
const pieChartRef = ref(null);
const barChartRef = ref(null);
let pieChart = null;
let barChart = null;

// 获取统计数据
const fetchStats = async () => {
    try {
        const { data } = await get_stats();
        if (data.code === 200) {
            stats.value = data.data;
            // console.log('stats',stats.value);
            
            updateCharts();
        } else {
            ElMessage.error(data.message || '获取统计数据失败');
        }
    } catch (error) {
        if (error.response?.status === 401) {
            ElMessage.error('请先登录管理员账号');
        } else {
            ElMessage.error('获取统计数据失败：' + error.message);
        }
    }
};

// 初始化图表
const initCharts = () => {
    // 初始化饼图
    pieChart = echarts.init(pieChartRef.value);
    // 初始化柱状图
    barChart = echarts.init(barChartRef.value);
    updateCharts();
};

// 更新图表数据
const updateCharts = () => {
    if (!pieChart || !barChart) return;

    // 饼图配置
    const pieOption = {
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '数据统计',
                type: 'pie',
                radius: '60%',
                data: [
                    { value: stats.value.userCount, name: '用户数量' },
                    { value: stats.value.petCount, name: '宠物数量' },
                    { value: stats.value.articleCount, name: '文章数量' },
                    { value: stats.value.postCount, name: '帖子数量' }
                ],
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };

    // 柱状图配置
    const barOption = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            data: ['用户数量', '宠物数量', '文章数量', '帖子数量']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: [
                    stats.value.userCount,
                    stats.value.petCount,
                    stats.value.articleCount,
                    stats.value.postCount
                ],
                type: 'bar',
                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(180, 180, 180, 0.2)'
                }
            }
        ]
    };

    pieChart.setOption(pieOption);
    barChart.setOption(barOption);
};

// 监听窗口大小变化
const handleResize = () => {
    pieChart?.resize();
    barChart?.resize();
};

// 组件挂载时获取数据和初始化图表
onMounted(() => {
    fetchStats();
    initCharts();
    window.addEventListener('resize', handleResize);
});

// 组件卸载时清理
onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
    pieChart?.dispose();
    barChart?.dispose();
});

// 监听数据变化更新图表
watch(stats, () => {
    updateCharts();
}, { deep: true });
</script>

<style scoped>
.admin-dashboard {
    padding: 5px;
}

.dashboard-title {
    margin-bottom: 24px;
    font-size: 24px;
    font-weight: 500;
    color: #303133;
}

.stats-card {
    height: 160px;
    transition: all 0.3s ease;
}

.stats-card:hover {
    transform: translateY(-5px);
}

.card-header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 16px;
    color: #606266;
}

.card-value {
    font-size: 36px;
    font-weight: bold;
    color: #303133;
    text-align: center;
    margin-top: 20px;
}

.el-icon {
    font-size: 20px;
}

.charts-row {
    margin-top: 20px;
}

.chart-card {
    margin-bottom: 20px;
}

.chart {
    height: 400px;
    width: 100%;
}
</style>